package com.note.back.controller;

import com.note.back.pojo.Category;
import com.note.back.pojo.Result;
import com.note.back.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success("分类添加成功");
    }

    @GetMapping("/list")
    public Result<List<Category>> list(){
        List<Category> cs = categoryService.list();
        return Result.success(cs,"分类查询成功");
    }

    @GetMapping("/detail/{id}")
    public Result<Category> detail(@PathVariable String id){
        Category c = categoryService.findById(Integer.valueOf(id));
        return Result.success(c,"查询成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success("分类修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        categoryService.delete(Integer.valueOf(id));
        return Result.success("分类删除成功");
    }
}
